﻿using Clipper.BLL.DataBaseBLL;
using Clipper.Model.DataBaseModel;
using Communication;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Clipper.ClipperUserControl
{
    /// <summary>
    /// UserArmMoveControl2.xaml 的交互逻辑
    /// </summary>
    public partial class UserArmMoveControl2 : UserControl
    {

        LocaltionPosition mlocaltionBll = new LocaltionPosition();
        public UserArmMoveControl2()
        {
            InitializeComponent();
        }

        private void button24_Click(object sender, RoutedEventArgs e)
        {
            LocationPosition detectModel = mlocaltionBll.GetModel("检测区");
            textBox22.Text = detectModel.XAxis.ToString();
            textBox23.Text = detectModel.YAxis.ToString();
            textBox25.Text = detectModel.XOffset.ToString();
            textBox26.Text = detectModel.YOffset.ToString();
            textBox.Text = detectModel.Z1_z2Offset.ToString();
            textBox24.Text = detectModel.Z1Axis.ToString();
            textBox1.Text = detectModel.Z2Axis.ToString();
        }

        private void button15_Click(object sender, RoutedEventArgs e)
        {
            #region //清洗区更新操作
            LocationPosition upCleanModel = new LocationPosition();
            if (!textBox20.Text.Equals(""))
            {
                upCleanModel.XAxis = Convert.ToInt32(textBox20.Text);
            }
            else
            {

                //MessageBox.Show("清洗区X轴坐标不能为空！！", "警告！！");
                if (MessageBoxResult.Yes == MessageBox.Show("清洗区X轴坐标不能为空！！", "警告！！", MessageBoxButton.YesNo, MessageBoxImage.Warning))
                {
                    return;
                }
            }

            if (!textBox21.Text.Equals(""))
            {
                upCleanModel.YAxis = Convert.ToInt32(textBox21.Text);
            }
            else
            {
                //MessageBox.Show("清洗区Y轴坐标不能为空！！","警告！！");
                if (MessageBoxResult.Yes == MessageBox.Show("清洗区Y轴坐标不能为空！！", "警告！！", MessageBoxButton.YesNo, MessageBoxImage.Warning))
                {
                    return;
                }

            }
            if (!textBox31.Text.Equals(""))
            {
                upCleanModel.ZAxis = Convert.ToInt32(textBox31.Text);
            }
            else
            {
                if (MessageBoxResult.Yes == MessageBox.Show("清洗区Z轴下移量不能为空！！", "警告！！", MessageBoxButton.YesNo, MessageBoxImage.Warning))
                {
                    return;
                }
            }
            if (!textBox30.Text.Equals("") && !textBox30.Text.Equals("0"))
            {
                upCleanModel.CleanTime = Convert.ToInt32(textBox30.Text);
            }
            else
            {
                if (MessageBoxResult.Yes == MessageBox.Show("清洗区时间不能为空！！", "提示", MessageBoxButton.YesNo, MessageBoxImage.Warning))
                {
                    return;
                }
            }

            upCleanModel.Place = "清洗区";
            bool com = mlocaltionBll.Update(upCleanModel);
            if (com)
            {
                MessageBox.Show("更新成功", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            else
            {
                MessageBox.Show("更新失败", "警告提示", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
            #endregion
        }

        private void button17_Click(object sender, RoutedEventArgs e)
        {
            #region //检测区的更新操作
            LocationPosition upDetectModel = new LocationPosition();
            if (!textBox22.Text.Equals(""))
            {
                upDetectModel.XAxis = Convert.ToInt32(textBox22.Text);
            }
            else
            {
                //MessageBox.Show("检测区的X轴坐标不能为空！！","警告！！");
                if (MessageBoxResult.Yes == MessageBox.Show("检测区的X轴坐标不能为空！！", "警告！！", MessageBoxButton.YesNo, MessageBoxImage.Warning))
                {
                    return;
                }

            }

            if (!textBox23.Text.Equals(""))
            {
                upDetectModel.YAxis = Convert.ToInt32(textBox23.Text);
            }
            else
            {
                //MessageBox.Show("检测区的Y轴坐标不能为空！！", "警告！！");
                if (MessageBoxResult.Yes == MessageBox.Show("检测区的Y轴坐标不能为空！！", "警告！！", MessageBoxButton.YesNo, MessageBoxImage.Warning))
                {
                    return;
                }
            }

            if (!textBox24.Text.Equals(""))
            {
                upDetectModel.Z1Axis = Convert.ToInt32(textBox24.Text);
            }
            else
            {
                //MessageBox.Show("检测区的Z轴下移量不能为空！！", "警告！！");
                if (MessageBoxResult.Yes == MessageBox.Show("检测区的Z1轴下移量不能为空！！", "警告！！", MessageBoxButton.YesNo, MessageBoxImage.Warning))
                {
                    return;
                }
            }

            if (!textBox1.Text.Equals(""))
            {
                upDetectModel.Z2Axis = Convert.ToInt32(textBox1.Text);
            }
            else
            {
                //MessageBox.Show("检测区的Z轴下移量不能为空！！", "警告！！");
                if (MessageBoxResult.Yes == MessageBox.Show("检测区的Z2轴下移量不能为空！！", "警告！！", MessageBoxButton.YesNo, MessageBoxImage.Warning))
                {
                    return;
                }
            }

            if (!textBox25.Text.Equals(""))
            {
                upDetectModel.XOffset = Convert.ToInt32(textBox25.Text);
            }
            else
            {
                //MessageBox.Show("检测区的X轴偏移量不能为空！！", "警告！！");
                if (MessageBoxResult.Yes == MessageBox.Show("检测区的X轴偏移量不能为空！！", "警告！！", MessageBoxButton.YesNo, MessageBoxImage.Warning))
                {
                    return;
                }
            }

            if (!textBox26.Text.Equals(""))
            {
                upDetectModel.YOffset = Convert.ToInt32(textBox26.Text);
            }
            else
            {
                //MessageBox.Show("检测区的Y轴的偏移量不能为空！！", "警告！！");
                if (MessageBoxResult.Yes == MessageBox.Show("检测区的Y轴偏移量不能为空！！", "警告！！", MessageBoxButton.YesNo, MessageBoxImage.Warning))
                {
                    return;
                }
            }

            if (!textBox.Text.Equals(""))
            {
                upDetectModel.Z1_z2Offset = Convert.ToInt32(textBox.Text);
            }
            else
            {
                //MessageBox.Show("检测区的Y轴的偏移量不能为空！！", "警告！！");
                if (MessageBoxResult.Yes == MessageBox.Show("检测区的Z1和Z2之间的偏移量不能为空！！", "警告！！", MessageBoxButton.YesNo, MessageBoxImage.Warning))
                {
                    return;
                }
            }

            upDetectModel.Place = "检测区";
            bool redata = mlocaltionBll.Update(upDetectModel);
            if (redata)
            {
                MessageBox.Show("更新成功", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            else
            {
                MessageBox.Show("更新失败！!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            #endregion
        }

        private void button23_Click(object sender, RoutedEventArgs e)
        {
            LocationPosition cleanmodel = mlocaltionBll.GetModel("清洗区");
            textBox20.Text = cleanmodel.XAxis.ToString();
            textBox21.Text = cleanmodel.YAxis.ToString();
            textBox31.Text = cleanmodel.ZAxis.ToString();
            textBox30.Text = cleanmodel.CleanTime.ToString();
        }

        private void button18_Click(object sender, RoutedEventArgs e)
        {
            //定位检测区Z1
            ArmMoveControl.MoveToPositionEnginner(Convert.ToInt32(textBox22.Text), Convert.ToInt32(textBox23.Text));
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Z1DetectDownDistance();
            Z1Detect++;

        }
        //检测区Z1下移量
        int Z1Detect = 1;
        public void Z1DetectDownDistance()
        {
            if (Z1Detect%2==0)
            {
                this.button1.Content = "Z1下移";
                ArmMoveControl.Z1ReturnZeroEnginner();
            }
            else
            {
                this.button1.Content = "Z1上移";
                ArmMoveControl.Z1DownDistanceEnginner(Convert.ToInt32(textBox24.Text));
            }
        }

        private void button21_Click(object sender, RoutedEventArgs e)
        {
            //Z2定位
            ArmMoveControl.MoveToPositionEnginner(Convert.ToInt32(textBox22.Text), Convert.ToInt32(textBox23.Text)+ Convert.ToInt32(textBox.Text));
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            Z2DetectDownDistance();
            Z2detect++;
        }
        int Z2detect = 1;
        public void Z2DetectDownDistance()
        {
            if (Z2detect%2==0)
            {
                this.button2.Content = "Z2下移";
                ArmMoveControl.Z2ReturnZeroEnginner();

            }
            else
            {
                this.button2.Content = "Z2上移";
                ArmMoveControl.Z2DownDistanceEnginner(Convert.ToInt32(textBox1.Text));
            }
        }

        private void button14_Click(object sender, RoutedEventArgs e)
        {
            ArmMoveControl.MoveToPositionEnginner(Convert.ToInt32(textBox20.Text), Convert.ToInt32(textBox21.Text));
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            Z1_Z2Clean();
            Z1_Z2CleanArea++;
        }

        int Z1_Z2CleanArea = 1;
        public void Z1_Z2Clean()
        {
            if (Z1_Z2CleanArea%2==0)
            {
                this.button.Content = "下移";
                ArmMoveControl.ReturnZ1_Z2ZeroEnginner();
            }
            else
            {
                this.button.Content = "上移";
                ArmMoveControl.Z1_Z2DownDistanceEnginner(Convert.ToInt32(textBox31.Text));
            }
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            OnePumpControl();
            onePump++;
        }

        //定义变量
        int onePump = 1;
        public void OnePumpControl()
        {

            if (onePump % 2 == 0)
            {
                this.button3.Content = "Z1开";
                Can.SendCan(88, "1OL0" + "\r");
                Can.SendCan(88, "1OL2" + "\r");
                Can.SendCan(88, "1OL5" + "\r");
                Can.SendCan(88, "1OL4" + "\r");
                Can.SendCan(88, "1OL8" + "\r");
                Can.SendCan(88, "1OL9" + "\r");
            }
            else
            {
                this.button3.Content = "Z1关";
                Can.SendCan(88, "1OH8" + "\r");
                Can.SendCan(88, "1OH9" + "\r");
                Can.SendCan(88, "1OH5" + "\r");
                Can.SendCan(88, "1OH4" + "\r");
                Can.SendCan(88, "1OH0" + "\r");
                Can.SendCan(88, "1OH2" + "\r");
            }

        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            TwoPumpControl();
            twoPump++;
        }
        //Z2 开启 和关闭的控制
        int twoPump = 1;
        public void TwoPumpControl()
        {
            if (twoPump % 2 == 0)
            {
                this.button4.Content = "Z2开";
                Can.SendCan(88, "1OL1" + "\r");
                //  my_can.SendCANData(88, "1OL3" + "\r");
                Can.SendCan(88, "1OL4" + "\r");
                Can.SendCan(88, "1OL9" + "\r");
            }
            else
            {
                this.button4.Content = "Z2关";
                Can.SendCan(88, "1OH9" + "\r");
                Can.SendCan(88, "1OH4" + "\r");
                Can.SendCan(88, "1OH1" + "\r");
            }
        }

        private void button5_Click(object sender, RoutedEventArgs e)
        {
            AllControlPump();
            allControl++;
        }

        //Z1 和 Z2 全部开启的控制
        int allControl = 1;
        public void AllControlPump()
        {
            if (allControl % 2 == 0)
            {
                ArmMoveControl.PumpResetInControl();  //开启泵工作
                this.button5.Content = "Z1Z2全开";
                Can.SendCan(88, "1OL0" + "\r");
                Can.SendCan(88, "1OL2" + "\r");
                Can.SendCan(88, "1OL5" + "\r");
                Can.SendCan(88, "1OL4" + "\r");
                Can.SendCan(88, "1OL8" + "\r");
                Can.SendCan(88, "1OL9" + "\r");

                Can.SendCan(88, "1OL1" + "\r");
                //  my_can.SendCANData(88, "1OL3" + "\r");
                //Can.SendCan(88, "1OL4" + "\r");
                //Can.SendCan(88, "1OL9" + "\r");
                //此处进行空气柱的重吸3000
                Thread.Sleep(1000);
                ArmMoveControl.PumpArispAirPower();

            }
            else
            {
                this.button5.Content = "Z1Z2全关";
                Can.SendCan(88, "1OH8" + "\r");
                Can.SendCan(88, "1OH9" + "\r");
                Can.SendCan(88, "1OH5" + "\r");
                Can.SendCan(88, "1OH4" + "\r");
                Can.SendCan(88, "1OH0" + "\r");
                Can.SendCan(88, "1OH2" + "\r");

                //Can.SendCan(88, "1OH9" + "\r");
                //Can.SendCan(88, "1OH4" + "\r");
                Can.SendCan(88, "1OH1" + "\r");
            }
        }

        private void button6_Click(object sender, RoutedEventArgs e)
        {
           // ArmMoveControl.CleanNeedle(50);
        }
    }
}
